<?php

/**
 * OAuth登录回调
 * 
 * @author ShuangYa
 * @package EUser
 * @category Controller
 * @link http://www.sylingd.com/
 * @copyright Copyright (c) 2015-2016 ShuangYa
 * @license http://lab.sylingd.com/go.php?name=euser&type=license
 */

namespace euser\controller;
use \Sy;
use \sy\base\Controller;
use \sy\lib\YHtml;
use \sy\lib\YSecurity;
use \euser\libs\option;
use \euser\libs\User;
use \euser\libs\OAuthClient;

class Oauthclient extends Controller {
	protected $oauth;
	public function __construct() {
		$this->oauth = unserialize(file_get_contents(Sy::$appDir . 'data/oauth.serialize'));
	}
	//跳转
	public function actionRedirect($requestId) {
		Sy::setMimeType('html', $requestId);
		$client = strtolower(Sy::$httpRequest[$requestId]->get['client']);
		//是否开启
		if (!in_array($client, $this->oauth['enable'], TRUE)) {
			Sy::$httpResponse[$requestId]->write('Disabled');
			return;
		}
		//初始化
		$clientObjName = '\\euser\\libs\\oauth\\' . $client;
		$clientObj = call_user_func($clientObjName . '::i', $this->oauth[$client]);
		$url = call_user_func([$clientObj, 'getAuthorizeUrl']);
		Sy::$httpResponse[$requestId]->write('<script type="text/javascript">window.location.href="' . $url . '";</script>');
	}
	//回调
	public function actionCallback($requestId) {
		Sy::setMimeType('html', $requestId);
		$client = strtolower(Sy::$httpRequest[$requestId]->get['client']);
		//是否开启
		if (!in_array($client, $this->oauth['enable'], TRUE)) {
			Sy::$httpResponse[$requestId]->write('Disabled');
			return;
		}
		//初始化
		$clientObjName = '\\euser\\libs\\oauth\\' . $client;
		$clientObj = call_user_func($clientObjName . '::i', $this->oauth[$client]);
		//获取信息
		$clientResult = call_user_func([$clientObj, 'callback'], Sy::$httpRequest[$requestId]->get['code']);
		var_dump($clientResult);
		Sy::$httpResponse[$requestId]->write('<script type="text/javascript">window.opener.OAuthCallback();</script>');
	}
}
